﻿namespace FIMTestCasesDriver
{
    using System;
    using System.Linq;
    using System.Linq.Expressions;

    public static class MetaverseHelper
    {
        private static readonly FIMEntities FIMEntities = new FIMEntities();
        
        public static bool TryToGetMVEntryByUniqueID(
            Expression<Func<mms_metaverse, bool>> selector,
            out mms_metaverse mvEntryOut)
        {
            try
            {
                mvEntryOut = FIMEntities.mms_metaverse.AsNoTracking().Single(selector);
                return true;
            }
            catch (InvalidOperationException ex)
            {
                // nothing is found 
                if (ex.Message.Equals("Sequence contains no elements"))
                {
                    mvEntryOut = null;
                    return false;
                }

                throw;
            }
        }

        public static bool DoesReferenceRelationshipExist(
            mms_metaverse source,
            mms_metaverse destination,
            string attributeName)
        {
            return (from reference in FIMEntities.mms_mv_link
             where reference.attribute_name.Equals(attributeName) &&
             reference.object_id == source.object_id &&
             reference.reference_id == destination.object_id
             select reference).Any();
        }
    }
}
